/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.t4.irc;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import com.t4.irc.net.packet.PacketFactory;

/**
 * GUI
 * @author T4_
 *
 */
public class GUI implements ActionListener, KeyListener, MouseListener {
	
	/** Logger instance for this class */
	private Logger log = new Logger(this.getClass());
	
	/** The main window */
    private JFrame window = new JFrame("jic: IRC Client");
    
    /** The container of all of the panels */
    private JPanel container = new JPanel(), topContainer = new JPanel(), midContainer = new JPanel(), southContainer = new JPanel();
    
    /** The menu bar */
    private JMenuBar menuBar = new JMenuBar();
    
    /** The main menu */
    private JMenu fileMenu = new JMenu("File"), ircMenu = new JMenu("IRC"), helpMenu = new JMenu("Help");
    
    /** The buttons to perform certain actions */
    private JMenuItem exitMenuItem = new JMenuItem("Exit"), connectMenuItem = new JMenuItem("Connect"), 
    		disconnectMenuItem = new JMenuItem("Disconnect"), 
    		ircNicknameMenuItem = new JMenuItem("Change Nickname"),
    		ircNamesMenuItem = new JMenuItem("Who's online?"),
    		aboutMenuItem = new JMenuItem("About");
    
    /** Separates menu items */
    private JSeparator separatorMenuItem = new JSeparator();
    
    /** The input field */
    private JTextField inputTextField = new JTextField();
    
    /** The output text area */
    private JTextArea outputTextArea = new JTextArea();
    
    /** The scroll pane for the output text area*/
    private JScrollPane outputScrollPane;
    

	/**
	 * Creates a new GUI
	 */
	public GUI() {
		window.setSize(875, 500);
		container.setLayout(new BorderLayout());
		
		/*
		fileMenu.add(connectMenuItem);
		connectMenuItem.addActionListener(this);
		*/
		
		fileMenu.add(disconnectMenuItem);
		disconnectMenuItem.addActionListener(this);
		
		fileMenu.add(separatorMenuItem);
		
		fileMenu.add(exitMenuItem);
		exitMenuItem.addActionListener(this);
		
		menuBar.add(fileMenu);
		

		ircMenu.add(ircNicknameMenuItem);
		ircNicknameMenuItem.addActionListener(this);
		
		
		ircMenu.add(ircNamesMenuItem);
		ircNamesMenuItem.addActionListener(this);
		
		menuBar.add(ircMenu);
		
		helpMenu.add(aboutMenuItem);
		aboutMenuItem.addActionListener(this);
		menuBar.add(helpMenu);
		
		topContainer.setLayout(new BorderLayout());
		topContainer.add(menuBar, BorderLayout.NORTH);
		
		outputTextArea.setEditable(false);
		outputTextArea.setPreferredSize(window.getSize());
		outputTextArea.setBackground(new Color(0xF0F0F0));
		outputScrollPane = new JScrollPane(outputTextArea);
		midContainer.setLayout(new GridLayout());
		midContainer.add(outputScrollPane, BorderLayout.NORTH);
		
		southContainer.setLayout(new BorderLayout());
		inputTextField.addKeyListener(this);
		southContainer.add(inputTextField, BorderLayout.SOUTH);

		
		container.add(topContainer, BorderLayout.NORTH);		
		container.add(midContainer, BorderLayout.CENTER);
		container.add(southContainer, BorderLayout.SOUTH);

	    window.add(container);
	    window.pack();
	    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    window.setVisible(true);		
	}


	public void pushText(String text) {
		if(text.length() > 138) {
			int parts = text.length()/138;
			for(int i = 0; i < parts; i++)
				pushText(text.substring(i * 138, (i + 1) * 138));			
		} else
			outputTextArea.append(" " + text + "\n");
	}
	
	@Override
	public void mouseClicked(MouseEvent event) {
		Object src = event.getSource();
		
	}
	
	@Override
	public void keyTyped(KeyEvent event) {
		Object src = event.getSource();
		final int ENTER_KEY = 10;
		if(src == inputTextField) {
			if(event.getKeyChar() == ENTER_KEY) {
				final String inputText = inputTextField.getText();
				log.debug("inputText: " + inputText);
				inputTextField.setText("");
				
				pushText(IRC.getConfiguration().getNickname() + ": " + inputText);
				PacketFactory.sendMessage(IRC.getConfiguration().getChannel(), inputText);
 
			}
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		Object src = event.getSource();
		if(src == exitMenuItem) {
			log.debug("Exit triggered");
			window.dispose();
		}
		
		if(src == connectMenuItem) {
			log.debug("Connect triggered");
			String nickname;
			nickname = JOptionPane.showInputDialog(window, "Enter your nickname:");
			log.debug("Nickname: " + nickname);
			IRC.getConfiguration().setNickname(nickname);
			//TODO: handle connection
		}
		
		if(src == disconnectMenuItem) {
			log.debug("Disconnect triggered");
			PacketFactory.quit("Disconnect");
			IRC.getConnector().close();
			pushText("Disconnected.");
		}

		if(src == aboutMenuItem) {
			log.debug("About triggered");
			JOptionPane.showMessageDialog(window, "IRC Client\ncreated by: T4_");
		}
		
		if(src == ircNicknameMenuItem) {
			log.debug("Changed nickname triggered");
			String nickname;
			nickname = JOptionPane.showInputDialog(window, "Enter your new nickname:");
			log.debug("Nickname: " + nickname);
			IRC.getConfiguration().setNickname(nickname);
			PacketFactory.nick(nickname);
			pushText("> Your name is now " + nickname);
		}
		
		if(src == ircNamesMenuItem) {
			pushText("> Online users:");
			PacketFactory.names(IRC.getConfiguration().getChannel());
		}

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {}
	
	@Override
	public void mouseExited(MouseEvent arg0) {}

	@Override
	public void mousePressed(MouseEvent arg0) {}

	@Override
	public void mouseReleased(MouseEvent arg0) {}

	@Override
	public void keyPressed(KeyEvent arg0) {}

	@Override
	public void keyReleased(KeyEvent arg0) {}

}
